﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include<memory>
#include"Scene.h"
#include "../ui/UiCore.h"
#include "../base/GameWorld.h"
#include "../ui/DefultWidget.h"
#include "../base/Debuger.h"
#include "../win32/IApplicationView.h"

namespace SkyEngine2d
{
	
	bool Scene::init()
	{
		if (!Node::init())
		{
			return false;
		}
	
		m_uiCore = GameWorld::getInstance()->getUiCore();
		
		auto p = DefultWidget::create();
	
		this->setDefaultWidget(p);
	
		return true;
	}
	
	std::shared_ptr<Scene> Scene::convertSceneSharedPtr()
	{
		return std::dynamic_pointer_cast<Scene>(shared_from_this());
	}
	
	void Scene::setDefaultWidget(Widget* widget)
	{
		m_defaultWidget = widget;
		m_uiCore->setDefaultWidget(widget);
	}
	
	Widget* Scene::getDefaultWidget() const
	{
		return m_defaultWidget;
	}
	
	void Scene::traversal(IRender* render)
	{
		this->visit(render, false);
	}
	
	void Scene::visit(IRender* render, bool parent_change_transfrom_mat)
	{
		//先渲染普通节点
		Node::visit(render, parent_change_transfrom_mat);
		//渲染界面
		auto  p = getDefaultWidget();
		if (p)
		{
			p->visit(render, parent_change_transfrom_mat);
		}
	}
	
	
	Scene::Scene(Node* parent /*= nullptr*/)
		:Node(parent)
		, m_uiCore(nullptr)
	{
		this->setContentSize(GameWorld::getInstance()->getAppView()->getWinSize());
	}
	
	void Scene::enterScene()
	{
		DebugInfor("enterScene");
	}
	
	void Scene::quitScene()
	{
		DebugInfor("quitScene");
	}
	
	Scene::~Scene()
	{
	}
	
}
